#include "ModalLayer.h"

/*********************| 一个用来表示一个层的点击状态的类  |*********************/
class TouchArrCell : public CCObject
{
public:
	static TouchArrCell* 		create(CCTouchDelegate* ly);
public:
	CCTouchDelegate*				pLayer;
	bool 				bTouch;
};

TouchArrCell*TouchArrCell::create(CCTouchDelegate* ly)
{
	if (!ly) return NULL;
	TouchArrCell* ret = new TouchArrCell();
	if (ret) {
		ret->pLayer = ly;
		ret->bTouch = false;
		ret->autorelease();
		return ret;
	} else {
		delete ret;
		return NULL;
	}
}

/*********************| func in ModayLayer |*********************/

bool ModalLayer::init(const int zOrder)
{
	bool bRet = false;
	do {
		CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 128)));
		_touchLayerArr = CCArray::createWithCapacity(2);
		CC_BREAK_IF(!_touchLayerArr);
		_touchLayerArr->retain();

		_pMenu = CCMenu::create();
		CC_BREAK_IF(!_pMenu);
		_pMenu->setPosition(CCPointZero);
		addACanTouchLayer(_pMenu);

		this->addChild(_pMenu, zOrder);

		bRet = true;
	}while(0);
	return bRet;
}

void ModalLayer::onEnter()
{
	CCLayerColor::onEnter();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 2, true);
}

void ModalLayer::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
	CCLayerColor::onExit();
}

bool ModalLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	if (_pTouch == NULL) {
		_pTouch = pTouch;
		TouchArrCell* pCell = NULL;
		CCObject* pObj = NULL;
		CCARRAY_FOREACH(_touchLayerArr, pObj) {
			pCell = (TouchArrCell*)pObj;
			if (pCell) {
				pCell->bTouch = pCell->pLayer->ccTouchBegan(pTouch, pEvent);
			}
		}
	}
	return true;
}

void ModalLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	if (_pTouch == pTouch ) {
		TouchArrCell* pCell = NULL;
		CCObject* pObj = NULL;
		CCARRAY_FOREACH(_touchLayerArr, pObj) {
			pCell = (TouchArrCell*)pObj;
			if (pCell && pCell->bTouch) {
				pCell->pLayer->ccTouchMoved(pTouch, pEvent);
			}
		}
	}
}

void ModalLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	if (_pTouch == pTouch) {
		TouchArrCell* pCell = NULL;
		CCObject* pObj = NULL;
		CCARRAY_FOREACH(_touchLayerArr, pObj) {
			pCell = (TouchArrCell*)pObj;
			if (pCell && pCell->bTouch) {
				pCell->pLayer->ccTouchEnded(pTouch, pEvent);
				pCell->bTouch = false;
			}
		}
		_pTouch = NULL;
	}
}

void ModalLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
	if (_pTouch == pTouch) {
		TouchArrCell* pCell = NULL;
		CCObject* pObj = NULL;
		CCARRAY_FOREACH(_touchLayerArr, pObj) {
			pCell = (TouchArrCell*)pObj;
			if (pCell && pCell->bTouch) {
				pCell->pLayer->ccTouchCancelled(pTouch, pEvent);
				pCell->bTouch = false;
			}
		}
		_pTouch = NULL;
	}
}

void ModalLayer::addACanTouchLayer(CCTouchDelegate* pLy)
{
	TouchArrCell* pCell = TouchArrCell::create(pLy);
	if (pCell) {
		_touchLayerArr->addObject(pCell);
	}
}

void ModalLayer::removeCanTouchLayer(const CCTouchDelegate* pLy)
{
	if (pLy) {
		TouchArrCell* pCell = NULL;
		CCObject* pObj = NULL;
		CCARRAY_FOREACH(_touchLayerArr, pObj) {
			if (pObj) {
				pCell = (TouchArrCell*)pObj;
				if (pCell->pLayer == pLy) {
					_touchLayerArr->removeObject(pObj);
				}
			}
		}
	}
}

int ModalLayer::show()
{
	if (this->getParent()) {
		CCLog("已经显示了，或者已经准备显示了");
		return 1;
	}
	CCScene* s = CCDirector::sharedDirector()->getRunningScene();
	if (s) {
		s->addChild(this);
		return 0;
	}
	return -1;
}

ModalLayer::ModalLayer()
{
	_pTouch = NULL;
	_pMenu = NULL;
}

ModalLayer::~ModalLayer()
{
	CC_SAFE_RELEASE(_touchLayerArr);
}


